<?php

/*
 * This file is part of the wiklite package.
 * (c) 2008 Guillermo Rauch <rauchg@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * wlToolkit provides basic utility methods. Some classes and functions are from symfony
 *
 * @package    wiklite
 * @subpackage core
 * @author     Guillermo Rauch <rauchg@gmail.com>
 * @version    SVN: $Id: wlRequest.class.php 6 2008-09-20 06:35:54Z rauchg $
 */

$wlErrors = array();

class wlRequest extends wlSingleton
{
  var $errors = array();
  
  function __construct() { }
  
  function & getInstance()
  {
    return parent::__getInstanceImp('wlRequest');
  }
  
  /**
   * Retrieves an error message.
   *
   * @param string An error name
   *
   * @return string An error message, if the error exists, otherwise null
   */
  function getError($name)
  {
    $retval = null;

    if (isset($this->errors[$name]))
      $retval = $this->errors[$name];

    return $retval;
  }

  /**
   * Retrieves an array of error names.
   *
   * @return array An indexed array of error names
   */
  function getErrorNames()
  {
    return array_keys($this->errors);
  }

  /**
   * Retrieves an array of errors.
   *
   * @return array An associative array of errors
   */
  function getErrors()
  {
    return $this->errors;
  }

  /**
   * Indicates whether or not an error exists.
   *
   * @param string An error name
   *
   * @return boolean true, if the error exists, otherwise false
   */
  function hasError($name)
  {
    return array_key_exists($name, $this->errors);
  }

  /**
   * Indicates whether or not any errors exist.
   *
   * @return boolean true, if any error exist, otherwise false
   */
  function hasErrors()
  {
    return (count($this->errors) > 0);
  }
  
  /**
   * Removes an error.
   *
   * @param string An error name
   *
   * @return string An error message, if the error was removed, otherwise null
   */
  function & removeError($name)
  {
    $retval = null;

    if (isset($this->errors[$name]))
    {
      $retval =& $this->errors[$name];

      unset($this->errors[$name]);
    }

    return $retval;
  }

  /**
   * Sets an error.
   *
   * @param string An error name
   * @param string An error message
   *
   */
  function setError($name, $message)
  {
    $this->errors[$name] = $message;
  }

  /**
   * Sets an array of errors
   *
   * If an existing error name matches any of the keys in the supplied
   * array, the associated message will be overridden.
   *
   * @param array An associative array of errors and their associated messages
   *
   */
  function setErrors($errors)
  {
    $this->errors = array_merge($this->errors, $errors);
  }
  
  /**
   * Sets an error from a Wordpress object
   *
   *
   * @param object An WP_Error object
   */
  function setWordpressError($obj)
  {
    $this->setError($obj->get_error_code(), $obj->get_error_message());
  }
  
}